#include "plugdirectory.h"

plugDirectory::plugDirectory( QString directory, QString path )
        : directory(directory), path(path)
{

}

QByteArray plugDirectory::listDir()
{
    QByteArray response;
    QDir dir( directory + path );
    QString text;
    //QFileIconProvider icon;

    text += QString("<table>");
    foreach (QFileInfo rootDir, dir.entryInfoList() )
    {
       /*
        QIcon ico = icon.icon( rootDir );
        QPixmap pixmap = ico.pixmap(QSize(15, 15));
        pixmap.save(tr("%1/%2.png").arg(directory).arg(rootDir.baseName()), "png");
        */
        if ( rootDir.fileName() == "." || rootDir.fileName() == ".." ) {
            // Nothing
        } else if( rootDir.isDir() ) {
            text += QString("<tr><td></td><td><a href=\"%1/\">%2</a></td><td></td></tr>").arg( rootDir.fileName().toUtf8() , QString(rootDir.fileName().toUtf8()));
        } else if( rootDir.isSymLink() ) {
        } else if( rootDir.isFile() ) {
            text += QString("<tr><td></td><td><a href=\"%1\">%2</a></td><td>%3</td></tr>").arg( QString(rootDir.fileName().toUtf8()), QString(rootDir.fileName().toUtf8()) ).arg(rootDir.size());
        }
    }
    text += QString("</table>");

    response.append("HTTP/1.1 200 OK");
    response.append("\r\n");
    response.append("Server: HTTPSrv");
    response.append("\r\n");
    response.append(QString("Content-lenght: %1").arg( text.size() ) );
    response.append("\r\n");
    response.append("Content-Type: text/html; charset=utf-8");
    response.append("\r\n");
    response.append("\r\n");
    response.append( text );
    return response;
}
